import api from '@/api/index'
import userApi from '@/api/systemManagement/user/index'
import { getToken, setToken, removeToken } from '@/utils/auth'
import de from "element-ui/src/locale/lang/de";

const user = {
  namespaced: true,
  state: {
    token: getToken(),
    name: '',
    avatar: '',
    unicode:'',
    permissions: [],
    userinfo:{}
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    //用户在第三方平台的头像，获取头像
    SET_UNICODE:(state, unicode)=>{
      state.unicode = unicode
    },
    SET_PERMISSIONS: (state, permissions) => {
      state.permissions = permissions
    },
    SET_USERINFO: (state, userinfo) => {
      state.userinfo = userinfo
    },
  },
  actions: {
    uploadAvatar({ state,dispatch,commit }, formData) {
      return new Promise((resolve, reject) => {
        userApi.uploadAvatar(formData).then(res => {
          let path = res.data.file_path
          let user = {...state.userinfo,...{avatar:path}}
          dispatch("changeInfo",user).then(res => {
            commit("SET_AVATAR",path)
            resolve(res)
          }).catch(error => {
            reject(error)
          })
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 登录
    Login({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        api.login(userInfo).then(res => {
          setToken(res.data.jwt)
          commit('SET_TOKEN', res.data.jwt);
          commit('SET_USERINFO', res.data);
          commit('SET_NAME', res.data.alias);
          let _avatar = res.data.avatar;
          if(res.data.unicode != ""){
            _avatar =  `https://i.hunnu.edu.cn/ums/avatar/${res.data.unicode}`;
          }
          commit('SET_AVATAR', _avatar);
          commit('SET_UNICODE', res.data.unicode);
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    loginByCode({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        api.loginByCode(userInfo).then(res => {
          setToken(res.data.jwt)
          commit('SET_TOKEN',res.data.jwt)
          commit('SET_USERINFO', res.data)
          commit('SET_NAME', res.data.alias)
          let _avatar = res.data.avatar;
          if(res.data.unicode != ""){
            _avatar =  `https://i.hunnu.edu.cn/ums/avatar/${res.data.unicode}`;
          }
          commit('SET_AVATAR', _avatar);
          commit('SET_UNICODE', res.data.unicode)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    loginOauth({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        api.authLogin(userInfo).then(res => {
          setToken(res.data.jwt)
          commit('SET_TOKEN',res.data.jwt)
          commit('SET_USERINFO', res.data)
          commit('SET_NAME', res.data.alias)
          let _avatar = res.data.avatar;
          if(res.data.unicode != ""){
            _avatar =  `https://i.hunnu.edu.cn/ums/avatar/${res.data.unicode}`;
          }
          commit('SET_AVATAR', _avatar);
          commit('SET_UNICODE', res.data.unicode)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    changeInfo({ commit },user) {
      return new Promise((resolve, reject) => {
        userApi.changeInfo(user).then(res => {
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 获取用户信息
    getUsersInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        userApi.getUsersInfo().then(res => {
          const user = res.data
          const avatar = user.avatar == "" ? require("@/assets/images/headPortrait.png") : user.avatar;
          commit('SET_NAME', user.alias)
          commit('SET_AVATAR', avatar)
          commit('SET_USERINFO', user)
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 退出系统
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        api.loginOut(state.token).then(res => {
          commit('SET_TOKEN', '')
          commit('SET_USERINFO',"")
          commit('SET_NAME',"")
          commit('SET_AVATAR',"")
          commit('SET_PERMISSIONS', "")
          removeToken()
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    }
  }
}

export default user
